package com.woniu.config;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import java.util.List;

/**
 * 初始化数据到布隆过滤器
 *
 * @author zrh
 */
@Order
@Slf4j
@Configuration
public class BloomFilterConfig implements InitializingBean {

    /**
     * redis - key
     */
    public final static String BLOOM_STRING_FILTER = "bloom:string:filter:";

    @Autowired
    private BloomRedisConfig service;

    /**
     * 初始化数据到布隆过滤器
     */
    @Override
    public void afterPropertiesSet() {
        // 模拟的数据
        List<Integer> list = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9);
        log.info("加载数据到布隆过滤器,size:{}", list.size());
        if (!CollectionUtils.isEmpty(list)) {
            list.stream().forEach(item -> {
//                LocalBloomFilter.put(item + "");
                service.addByBloomFilter(BLOOM_STRING_FILTER, item + "");
            });
        }
    }
}
